package com.cskaoyan.wordmemorize.controller.admin;


import com.cskaoyan.wordmemorize.aop_whh.annotation.OperateLog;
import com.cskaoyan.wordmemorize.auth.StpKit;
import com.cskaoyan.wordmemorize.common.result.Result;
import com.cskaoyan.wordmemorize.dto.admin.*;
import com.cskaoyan.wordmemorize.request.BatchDeleteCommand;
import com.cskaoyan.wordmemorize.request.VocCommand;
import com.cskaoyan.wordmemorize.request.VocPageRequest;
import com.cskaoyan.wordmemorize.service.FileUploadService;
import com.cskaoyan.wordmemorize.service.VocService;
import io.swagger.v3.oas.annotations.Parameter;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import org.springframework.ai.chat.client.ResponseEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.ClassPathResource;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.net.http.HttpHeaders;
import java.util.List;

@RestController
public class VocController {

    @Autowired
    VocService vocService;

    /**
     * 分页查询单词列表
     * @param pageQuery 分页查询参数
     * @return 分页结果
     */
    @GetMapping("/admin/voc/list")
    public Result<PageDTO<VocDTO>> getVocabularies(VocPageRequest pageQuery) {
            PageDTO<VocDTO> result = vocService.getVocabularies(pageQuery);
            return Result.ok(result);
    }
    //添加单词(√)
    @OperateLog(dateType = "词汇",operateType = 1)
    @PostMapping("/admin/voc")
    public Result<Void> addVoc(@RequestBody @Valid VocCommand command){
         vocService.addVoc(command);
         return Result.ok();
    }
    //修改单词(√)
    @OperateLog(dateType = "词汇",operateType = 2)
    @PutMapping("/admin/voc")
    public Result<Void> updateVoc(@RequestBody @Valid VocCommand command){
        vocService.updateVoc(command);
        return Result.ok();
    }
    //删除单词(√)
    @OperateLog(dateType = "词汇",operateType = 3)
    @DeleteMapping("/admin/voc/{vocId}")
    public Result deleteVocabulary(@PathVariable("vocId") Long vocId){
        vocService.deleteVocabulary(vocId);
        return Result.ok();
    }
    //删除例句(√)
    @OperateLog(dateType = "词汇例句",operateType = 3)
    @DeleteMapping("/admin/voc/example/{id}")
    public Result deleteExample(@PathVariable("id") Long vocId){
        vocService.deleteExample(vocId);
        return Result.ok();
    }
    //删除单词释义(√)
    @OperateLog(dateType = "词汇释义",operateType = 3)
    @DeleteMapping("/admin/voc/interpretation/{id}")
    public Result deleteVocMeaning(@PathVariable("id") Long vocId){
        vocService.deleteInterpretation(vocId);
        return Result.ok();
    }
   //批量删除单词(√)
   @OperateLog(dateType = "词汇",operateType = 4)
    @DeleteMapping("/admin/voc")
    public Result deleteVocabularies(@RequestBody BatchDeleteCommand command){
         vocService.deleteVocabularies(command);
        return Result.ok();
    }
    //获取单词-章节-书籍关系列表(√)
    @GetMapping("/admin/voc/relations/{vocId}")
    public Result<List<VocChapBookDTO>> getVocRelations(@PathVariable("vocId") Long vocId) {
        List<VocChapBookDTO> relations = vocService.getVocRelations(vocId);
        return Result.ok(relations);
    }
    //删除书籍-章节-单词关系
    @OperateLog(dateType = "书籍-章节-单词关系",operateType = 3)
    @DeleteMapping("/admin/voc/relation/{id}")
    public Result deleteRelation(@PathVariable("id") Long vocId ){
        vocService.deleteVocRelation(vocId);
        return Result.ok();
    }
    //校验上传单词的excel表格(√)
    @OperateLog(dateType = "excel",operateType = 6)
    @PostMapping("/admin/voc/excel/validate")
    public Result vocExcelValidate(@RequestParam("vocExcel") MultipartFile file) throws IOException {
        vocService.vocExcelValidate(file);
        return Result.ok();
    }
    //通过excel上传单词
    @OperateLog(dateType = "excel",operateType = 6)
    @PostMapping("/admin/voc/excel")
    public Result<ExcelAddOperateDTO> addVocExcel(@RequestParam("vocExcel")
                                    MultipartFile file, Long userId) throws IOException {
//        userId = StpKit.ADMIN.getLoginIdAsLong();
        ExcelAddOperateDTO result = vocService.addVocExcel(file, userId);
        return Result.ok(result);
    }
    @Resource
    private FileUploadService fileUploadService;
    //上传单词对应的音频文件
    @OperateLog(dateType = "音频",operateType = 5)
    @PostMapping("/admin/voc/file")
    public Result<String> uploadVocFile(MultipartFile file) throws IOException {
        String result = fileUploadService.upload(file);
        return Result.ok(result);
    }
    //删除单词对应的音频文件
    @OperateLog(dateType = "音频",operateType = 4)
    @DeleteMapping("/admin/voc/file/{type}/{id}")
    public Result<String> deleteVocFile(@PathVariable("id")String id,@PathVariable("type") String type) throws IOException {
        fileUploadService.deleteFile(id);
        return Result.ok();
    }


}
